﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.ComponentModel;

namespace LOMBARDISPICK.Web.Helpers
{
	public static class HtmlExtensions
	{
		public static MvcHtmlString RequiredFieldFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
		{
			// Get the metadata for the data annontations model
			var metadata = DataAnnotationsModelMetadata.FromLambdaExpression(expression, html.ViewData);

			// If the field is required generate label with an asterix
			if (metadata.IsRequired)
			{
				var spanText = "*";
				var tag = new TagBuilder("span");
				tag.MergeAttribute("class", "DenoteRequired");
				//tag.MergeAttributes(new Dictionary<string, object> { { "style", "color: red; margin-left: -5px; margin-right: 5px; vertical-align: top;" } });
				tag.SetInnerText(spanText);
				return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
			}
			return null;
		}
	}
}